package com.ruoyi.dataAnalysis.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.dataAnalysis.domain.DataResponse;
import com.ruoyi.dataAnalysis.domain.InsectStatistic;
import com.ruoyi.dataAnalysis.domain.InsectType;
import com.ruoyi.dataAnalysis.service.DataAnalysisService;
import com.ruoyi.utils.MyDateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author LiSuxuan
 * @date 2021年 04月 27日 11:10
 */
@Api("dataAnalysis")
@Slf4j
@RestController
@RequestMapping("/data")
public class DataAnalysisController {

    @Autowired
    private DataAnalysisService dataAnalysisService;

    /**
     * 时间段内发生总量
     *
     * @return
     */
    @ApiOperation("时间段内虫子发生总量，接口参数起止时间，例如：http://localhost:8080/data/total?params%5BbeginTime%5D=2021-04-07&params%5BendTime%5D=2021-04-07")
    @GetMapping("/total")
    public AjaxResult insectTotal(DataResponse dataResponse) {

        List<String> list = dataAnalysisService.selectInsectTotalList(dataResponse);
        DataAnalysisController dataAnalysisController = new DataAnalysisController();
        InsectType insectType = dataAnalysisController.json2InsectType(list);
        return AjaxResult.success(insectType);
    }


    /**
     * 不同时间的发生量, 根据起止时间计算阶段时间内 每天虫子的数量
     *
     * @param startTime 开始时间 示例：2021-05-03
     * @param endTime   结束时间
     * @return 查询列表
     */
    @ApiOperation("不同时间发生量，startTime 开始时间 示例:http://localhost:8080/data/difTime?startTime=2021-04-04&endTime=2021-04-07")
    @GetMapping("/difTime")
    public AjaxResult insectDiffTime(String startTime, String endTime) {
        /* 计算时间间隔，按 天 存入 集合 */
        List<String> dates = MyDateUtil.calculateTimeInterval(startTime, endTime);
        if (dates == null) {
            return AjaxResult.error("输入日期有误!");
        }
        /* 存放结果数据集 */
        List<InsectStatistic> statistics = new ArrayList<>();
        DataAnalysisController dataAnalysisController = new DataAnalysisController();
        for (String date : dates) {
            List<String> list = dataAnalysisService.selectInsectByDay(date);
            InsectType insectType = dataAnalysisController.json2InsectType(list);
            InsectStatistic insectStatistic = new InsectStatistic();
            insectStatistic.setInsectType(insectType);
            insectStatistic.setDay(date);
            insectStatistic.setSum(insectType.getA1() + insectType.getA2() + insectType.getA3() + insectType.getA4() + insectType.getA10() +
                    insectType.getA12() + insectType.getA21() + insectType.getA22() + insectType.getA41() + insectType.getA102() + insectType.getA122() +
                    insectType.getB13() + insectType.getB28() + insectType.getC21() + insectType.getE12() + insectType.getE40() + insectType.getE90() +
                    insectType.getE120() + insectType.getG21() +
                    insectType.getBbfs() + insectType.getHfs() +
                    insectType.getOther());
            statistics.add(insectStatistic);
        }
        return AjaxResult.success(statistics);
    }


    /**
     * 将数据库中的JSON格式转换为对象格式
     *
     * @param list list列表
     * @return InsectType类型
     */
    public InsectType json2InsectType(List<String> list) {

        int bbfs = 0;
        int hfs = 0;
        int other = 0;

        int a1 = 0;
        int a2 = 0;
        int a3 = 0;
        int a4 = 0;
        int a10 = 0;
        int a12 = 0;
        int a21 = 0;
        int a22 = 0;
        int a41 = 0;
        int a102 = 0;
        int a122 = 0;
        int b13 = 0;
        int b28 = 0;
        int c21 = 0;
        int e12 = 0;
        int e40 = 0;
        int e90 = 0;
        int e120 = 0;
        int g21 = 0;


        for (String s : list) {
            if (s != null) {
                InsectType totalNum = JSON.parseObject(s, InsectType.class);
                bbfs += totalNum.getBbfs();
                hfs += totalNum.getHfs();
                other += totalNum.getOther();
                a1 += totalNum.getA1();
                a2 += totalNum.getA2();
                a3 += totalNum.getA3();
                a4 += totalNum.getA4();
                a10 += totalNum.getA10();
                a12 += totalNum.getA12();
                a21 += totalNum.getA21();
                a22 += totalNum.getA22();
                a41 += totalNum.getA41();
                a102 += totalNum.getA102();
                a122 += totalNum.getA122();
                b13 += totalNum.getB13();
                b28 += totalNum.getB28();
                c21 += totalNum.getC21();
                e12 += totalNum.getE12();
                e40 += totalNum.getE40();
                e90 += totalNum.getE90();
                e120 += totalNum.getE120();
                g21 += totalNum.getG21();
            }
        }
        return new InsectType(bbfs, hfs, a1, a2, a3, a4, a10, a12, a21, a22, a41, a102, a122, b13, b28, c21, e12, e40, e90, e120, g21, other);
    }


}
